//
//  ZNEmoticon.swift
//  ZNWeibo
//
//  Created by FunctionMaker on 2017/9/7.
//  Copyright © 2017年 FunctionMaker. All rights reserved.
//

import UIKit

/// 表情模型
class ZNEmoticon: NSObject {
    
    /// 表情模型 false: 图片表情 true: emoji
    var type = false
    /// 表情文字，发送给新浪服务器(节约流量)
    var chs: String?
    /// 表情图片名称，用于本地的图文混排
    var png: String?
    /// emoji的十六进制编码
    var code: String?
    
    /// 表情文件目录
    var directory: String?
    
    /// `图片`表情对应的图片
    var image: UIImage? {
        if type {
            return nil
        }
        
        guard let directory = directory,
              let png = png
        else {
            return nil
        }
        
        return UIImage(named: "\(directory)/\(png)", in: Bundle.main.emoticonsBundle, compatibleWith: nil)
    }
    
    func imageText(font: UIFont) -> NSAttributedString {
        guard let image = image else {
            return NSAttributedString(string: "")
        }
        
        let attachment = NSTextAttachment()
        let height = font.lineHeight
        
        attachment.image = image
        attachment.bounds = CGRect(x: 0, y: -4, width: height, height: height)
        
        return NSAttributedString(attachment: attachment)
    }
    
    override var description: String {
        return yy_modelDescription()
    }
}
